package com.ysd.lis.service.lsafe.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.mapper.lsafe.LsafeItemMapper;
import com.ysd.lis.entity.lsafe.LsafeItem;
import com.ysd.lis.service.lsafe.LsafeItemService;
import com.ysd.lis.request.lsafe.LsafeItemParam;
import org.springframework.stereotype.Service;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ysd.common.Result;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.lis.entity.SysUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安全项目管理(LsafeItem)表服务实现类
 *
 * @author leiming
 * @since 2024-12-05 19:02:38
 */
@Service("lsafeItemService")
public class LsafeItemServiceImpl extends ServiceImpl<LsafeItemMapper, LsafeItem> implements LsafeItemService {

    private MPJLambdaWrapper<LsafeItem> initWrapper(){
        MPJLambdaWrapper<LsafeItem> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        		//ID 
		lambdaQueryWrapper.selectAs(LsafeItem::getId, LsafeItem::getId);
		//项目名称 
		lambdaQueryWrapper.selectAs(LsafeItem::getLsiName, LsafeItem::getLsiName);
		//项目编码 
		lambdaQueryWrapper.selectAs(LsafeItem::getCode, LsafeItem::getCode);
		//项目分类 
		lambdaQueryWrapper.selectAs(LsafeItem::getItemClass, LsafeItem::getItemClass);
		//安全等级 
		lambdaQueryWrapper.selectAs(LsafeItem::getItemGrade, LsafeItem::getItemGrade);
		//记录单位 
		lambdaQueryWrapper.selectAs(LsafeItem::getItemUnit, LsafeItem::getItemUnit);
		//项目说明 
		lambdaQueryWrapper.selectAs(LsafeItem::getDescribes, LsafeItem::getDescribes);
		//安全要求 
		lambdaQueryWrapper.selectAs(LsafeItem::getSafetyAsks, LsafeItem::getSafetyAsks);
		//状态 
		lambdaQueryWrapper.selectAs(LsafeItem::getStatus, LsafeItem::getStatus);
		//机构 
		lambdaQueryWrapper.selectAs(LsafeItem::getOrgId, LsafeItem::getOrgId);
		//所属科室 
		lambdaQueryWrapper.selectAs(LsafeItem::getDeptCode, LsafeItem::getDeptCode);
		//删除标记 
		lambdaQueryWrapper.selectAs(LsafeItem::getDelFlag, LsafeItem::getDelFlag);
		//添加人 
		lambdaQueryWrapper.selectAs(LsafeItem::getCreator, LsafeItem::getCreator);
		//添加时间 
		lambdaQueryWrapper.selectAs(LsafeItem::getCreateTime, LsafeItem::getCreateTime);
		//编辑人 
		lambdaQueryWrapper.selectAs(LsafeItem::getEditor, LsafeItem::getEditor);
		//编辑时间 
		lambdaQueryWrapper.selectAs(LsafeItem::getEditTime, LsafeItem::getEditTime);
		//删除时间 
		lambdaQueryWrapper.selectAs(LsafeItem::getDelTime, LsafeItem::getDelTime);
        return lambdaQueryWrapper;
    }

    @Override
    public Result findPageList(LsafeItemParam pageParam) {
        Page<LsafeItem> pageList=null;
        LambdaQueryWrapper<LsafeItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<LsafeItem> page = new Page<LsafeItem>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.eq(LsafeItem::getDelFlag, 0);
        /*if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LsafeItem::getBoxDoor, pageParam.getKeywords())
                    .or().like(LsafeItem::getModelName, pageParam.getKeywords())
                    .or().like(LsafeItem::getBrandName, pageParam.getKeywords()));
        }*/
        lambdaQueryWrapper.orderByAsc(LsafeItem::getCreateTime);
        
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(LsafeItem::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result findListByGId(LsafeItemParam pageParam) {
        Page<LsafeItem> pageList=null;
        LambdaQueryWrapper<LsafeItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<LsafeItem> page = new Page<LsafeItem>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        /*if(StringUtils.isNotBlank(pageParam.getDeptName())){
            lambdaQueryWrapper.eq(LsafeItem::getDeptName, pageParam.getDeptName());
        }
        lambdaQueryWrapper.eq(LsafeItem::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LsafeItem::getBoxDoor, pageParam.getKeywords())
                    .or().like(LsafeItem::getModelName, pageParam.getKeywords())
                    .or().like(LsafeItem::getBrandName, pageParam.getKeywords()));
        }*/
        lambdaQueryWrapper.orderByAsc(LsafeItem::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(LsafeItem::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result getInfoById(String id) {
        LsafeItem LsafeItem=this.baseMapper.selectById(id);
        return Result.succ(1, "获取信息成功", LsafeItem);
    }

    @Override
    public Result add(LsafeItemParam param) {
        LsafeItem info=new LsafeItem();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setCreator(sysUser.getId());
                param.setOrgId(sysUser.getOrgId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setCreateTime(new Date());
        BeanUtil.copyProperties(param,info);
//        info.setDelFlag(0);
//        info.setStatus(1);
        this.baseMapper.insert(info);
        return Result.succ(1, "添加信息成功", null);
    }

    @Override
    public Result edit(LsafeItemParam param) {
        Map returnMap = new HashMap();
        LsafeItem info=new LsafeItem();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setEditor(sysUser.getId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setEditTime(new Date());
        BeanUtil.copyProperties(param,info);
        this.baseMapper.updateById(info);
        returnMap.put("id", param.getId());
        return Result.succ(1, "修改信息成功", returnMap);
    }

    @Override
    public Result delete(List<String> ids) {
        Result result;
        try {
            boolean b = this.removeByIds(ids);
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result updelete(List<String> ids) {
        Result result;
        LambdaUpdateWrapper<LsafeItem> lambdaUpdateWrapper = new UpdateWrapper<LsafeItem>().lambda();
        // set 表示要更新的字段， eq 表示  WHERE 条件
        lambdaUpdateWrapper.set(LsafeItem::getDelFlag,1)
                .in(LsafeItem::getId, ids);
        this.baseMapper.update(new LsafeItem(), lambdaUpdateWrapper);

        try {
            int b= this.baseMapper.update(new LsafeItem(), lambdaUpdateWrapper);
            if (b>0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }
    
    @Transactional
    @Override
    public Result saveOrUpdateList(LsafeItemParam param) {
        Result result;
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
               return Result.fail(304, "请重新登陆", "");
            }
            for (LsafeItem info : param.getInfoList()) {
                if(StringUtils.isNotBlank(info.getId())){
                    info.setEditor(sysUser.getId());
                    info.setEditTime(new Date());
                    this.baseMapper.updateById(info);
                }else{
                    info.setCreator(sysUser.getId());
                    info.setOrgId(sysUser.getOrgId());
                    info.setCreateTime(new Date());
//                    info.setDelFlag(0);
//                    info.setStatus(1);
                    this.baseMapper.insert(info);
                }
            }
            Boolean b= this.saveOrUpdateBatch(param.getInfoList());
            if (b) {
                result = Result.succ(1, "保存成功", null);
            } else {
                result = Result.fail(400, "保存失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "保存失败", "");
        }
        return result;
    }
}

